package com.ly.heart.service.impl;

import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.core.domain.entity.SysDept;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.ActTaskCommentEntity;
import com.ly.heart.domain.SaleMakedDetailEntity;
import com.ly.heart.domain.SaleMarkedCustomerEntity;
import com.ly.heart.domain.dto.SaleMarkedDto;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.common.core.domain.ActCurrentTaskVo;
import com.ly.heart.domain.vo.SaleMarkedPriceVo;
import com.ly.heart.service.*;
import com.ly.heart.util.ChineseToPinyin;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.system.service.ISysDeptService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.SaleMarkedPriceMapper;
import com.ly.heart.domain.SaleMarkedPriceEntity;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 销售价目Service业务层处理
 *
 * @author wutao
 * @date 2024-07-15
 */
@Service
public class SaleMarkedPriceServiceImpl extends ServiceImpl<SaleMarkedPriceMapper, SaleMarkedPriceEntity> implements ISaleMarkedPriceService {

    @Resource
    private SaleMarkedPriceMapper saleMarkedPriceMapper;
    @Resource
    private IActModelService actModelService;
    @Resource
    private ISaleMarkedCustomerService saleMarkedCustomerService;
    @Resource
    private ISaleMakedDetailService saleMarkedDetailService;
    @Resource
    private ISysDeptService sysDeptService;
    @Resource
    private IActModelService iActModelService;
    @Resource
    private IActTaskCommentService actTaskCommentService;

    /**
     * 查询销售价目
     *
     * @param id 销售价目ID
     * @return 销售价目
     */
    @Override
    public SaleMarkedPriceEntity selectSaleMarkedPriceById(String id) {
        SaleMarkedPriceEntity saleMarkedPriceEntity = saleMarkedPriceMapper.selectSaleMarkedPriceById(id);
        List<SaleMarkedCustomerEntity> customerEntities = saleMarkedCustomerService.list(new LambdaQueryWrapper<SaleMarkedCustomerEntity>().eq(SaleMarkedCustomerEntity::getMarkedPriceId, id));
        saleMarkedPriceEntity.setSaleMarkedCustomerEntities(customerEntities);
        List<SaleMakedDetailEntity> markedDetailEntities = saleMarkedDetailService.list(new LambdaQueryWrapper<SaleMakedDetailEntity>()
                .eq(SaleMakedDetailEntity::getMarkedPriceId, id)
                .eq(SaleMakedDetailEntity::getLineState, ConstUtil.STATE_FINISH));
        saleMarkedPriceEntity.setSaleMakedDetailEntities(markedDetailEntities);
        return saleMarkedPriceEntity;
    }

    /**
     * 查询销售价目列表
     *
     * @param saleMarkedPrice 销售价目
     * @return 销售价目
     */
    @Override
    public List<SaleMarkedPriceEntity> selectSaleMarkedPriceList(SaleMarkedPriceEntity saleMarkedPrice) {
        return saleMarkedPriceMapper.selectSaleMarkedPriceList(saleMarkedPrice);
    }

    /**
     * 新增销售价目
     *
     * @param saleMarkedPrice 销售价目
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertSaleMarkedPrice(SaleMarkedPriceEntity saleMarkedPrice) {
        String tenderId = SnowflakeIdWorker.getInstance().nextStringId();
        Date nowDate = DateUtils.getNowDate();
        saleMarkedPrice.setId(tenderId);
        saleMarkedPrice.setCreateTime(DateUtils.getNowDate());
        saleMarkedPrice.setDeptId(CurrentUserUtil.getCompanyId());
        saleMarkedPrice.setState("0");
        StartProcessDto startDto = new StartProcessDto().setBusinessId(tenderId).setBusinessType(saleMarkedPrice.getBusinessType()).setModelId(saleMarkedPrice.getModelId())
                .setBusinessName("销售价目表_".concat(tenderId));
        String instanceId = actModelService.startProcess(startDto);
        saleMarkedPrice.setInstanceId(instanceId);
        List<SaleMakedDetailEntity> saleMakedDetailEntities = saleMarkedPrice.getSaleMakedDetailEntities();
        if (CollectionUtils.isEmpty(saleMakedDetailEntities)) {
            return AjaxResult.error("请添加销售价目明细");
        }

        if (StringUtils.isEmpty(saleMarkedPrice.getPriceCode())) {
            String companyId = CurrentUserUtil.getCompanyId();
            SysDept sysDept = sysDeptService.getById(companyId);
            int count = this.count();
            String countStr = String.format("%06d", count);
            String firstLetter = ChineseToPinyin.getFirstLetter(sysDept.getDeptName());
            String orderNumber = firstLetter.concat("_XSJM_").concat(countStr);
            saleMarkedPrice.setPriceCode(orderNumber);
        }
        if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedCustomerEntities())) {
            saleMarkedPrice.getSaleMarkedCustomerEntities().forEach(s -> {
                s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(tenderId);
                s.setType(ConstUtil.SALE_PRICE_TYPE_CUSTOMER);
                saleMarkedCustomerService.save(s);
            });

        }
        if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedSaleEntities())) {
            saleMarkedPrice.getSaleMarkedSaleEntities().forEach(s -> {
                s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(tenderId);
                s.setType(ConstUtil.SALE_PRICE_TYPE_SALE);
                saleMarkedCustomerService.save(s);
            });
        }
        if (!CollectionUtils.isEmpty(saleMakedDetailEntities)) {
            Map<String, List<SaleMakedDetailEntity>> collect = saleMakedDetailEntities.stream().collect(Collectors.groupingBy(SaleMakedDetailEntity::getCode));
            //循环collect
            collect.forEach((k, list) -> {
                boolean isValid = list.stream()
                        .allMatch(item -> list.stream()
                                .noneMatch(other -> other != item &&
                                        item.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().isBefore(other.getLoseDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) &&
                                        item.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().isAfter(other.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate())));
                if (!isValid) {
                    throw new RuntimeException("价目详情生效日期不能交叉");
                }
            });
            AtomicInteger i = new AtomicInteger(1);
            saleMakedDetailEntities.forEach(s -> {
                String nextedId = SnowflakeIdWorker.getInstance().nextStringId();
//                StartProcessDto startProcessDto = new StartProcessDto().setBusinessId(nextedId).setBusinessType(s.getBusinessType()).setModelId(s.getModelId())
//                        .setBusinessName("销售价目表详情行_".concat(nextedId));
//                String instanceSonId = actModelService.startProcess(startProcessDto);
                s.setLineState("0");
//                s.setInstanceId(instanceSonId);
                String countStr = String.format("%06d", i.get());
//                s.setCode(saleMarkedPrice.getPriceCode().concat("_DETAIL_").concat(countStr));
                s.setId(nextedId);
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(tenderId);
                i.getAndIncrement();
                boolean save = saleMarkedDetailService.save(s);
                if (!save) {
                    throw new RuntimeException("价目表保存失败");
                }
            });
        }
        boolean save = this.save(saleMarkedPrice);
        if (!save) {
            return AjaxResult.error("价目表保存失败");
        } else {
            return AjaxResult.success();
        }

    }


    /**
     * 修改销售价目
     *
     * @param saleMarkedPrice 销售价目
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateSaleMarkedPrice(SaleMarkedPriceEntity saleMarkedPrice) {
        Date nowDate = DateUtils.getNowDate();
        SaleMarkedPriceEntity priceEntity = this.getById(saleMarkedPrice.getId());
        if (ConstUtil.IS_QUOTE_YES.equals(priceEntity.getIsQuote())){
            return AjaxResult.error("该价目已使用，不能修改");
        }
        if (!ConstUtil.STATE_IN_DRAFT.equals(saleMarkedPrice.getState())){
            return AjaxResult.error("只允修改暂存状态的价目");
        }
        saleMarkedPrice.setUpdateTime(nowDate);
        if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedCustomerEntities())) {
            //先删除再添加
            saleMarkedCustomerService.remove(new LambdaQueryWrapper<>(SaleMarkedCustomerEntity.class).eq(SaleMarkedCustomerEntity::getMarkedPriceId, saleMarkedPrice.getId()));
            saleMarkedPrice.getSaleMarkedCustomerEntities().forEach(s -> {
                s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(saleMarkedPrice.getId());
                s.setType(ConstUtil.SALE_PRICE_TYPE_CUSTOMER);
            });
            boolean saved = saleMarkedCustomerService.saveBatch(saleMarkedPrice.getSaleMarkedCustomerEntities());
            if (!saved) {
                return AjaxResult.error("价目表提交失败");
            }
        }
        if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedSaleEntities())) {
            //先删除再添加
            saleMarkedCustomerService.remove(new LambdaQueryWrapper<>(SaleMarkedCustomerEntity.class).eq(SaleMarkedCustomerEntity::getMarkedPriceId, saleMarkedPrice.getId()));
            saleMarkedPrice.getSaleMarkedSaleEntities().forEach(s -> {
                s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(saleMarkedPrice.getId());
                s.setType(ConstUtil.SALE_PRICE_TYPE_SALE);
            });
            boolean saved = saleMarkedCustomerService.saveBatch(saleMarkedPrice.getSaleMarkedSaleEntities());
            if (!saved) {
                return AjaxResult.error("价目表提交失败");
            }
        }
        if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMakedDetailEntities())) {
            //先删除再添加
            saleMarkedDetailService.remove(new LambdaQueryWrapper<>(SaleMakedDetailEntity.class).eq(SaleMakedDetailEntity::getMarkedPriceId, saleMarkedPrice.getId()));
            saleMarkedPrice.getSaleMakedDetailEntities().forEach(s -> {
                s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(saleMarkedPrice.getId());
            });
            boolean saved = saleMarkedDetailService.saveBatch(saleMarkedPrice.getSaleMakedDetailEntities());
            if (!saved) {
                return AjaxResult.error("价目表提交失败");
            }
        }
        saleMarkedPrice.setUpdateTime(nowDate);
        boolean updated = this.updateById(saleMarkedPrice);
        if (!updated) {
            return AjaxResult.error("修改失败");
        }

        return AjaxResult.success("修改成功");
    }

    /**
     * 批量删除销售价目
     *
     * @param ids 需要删除的销售价目ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteSaleMarkedPriceByIds(String[] ids) {
        List<SaleMarkedPriceEntity> list = this.list(new LambdaQueryWrapper<>(SaleMarkedPriceEntity.class).in(SaleMarkedPriceEntity::getId, ids));
        //判断是否有已引用的数据
        if (list.stream().anyMatch(s -> ConstUtil.IS_QUOTE_YES.equals(s.getIsQuote()))) {
            return AjaxResult.error("删除数据中存在已引用的数据，不能删除");
        }
        if (!saleMarkedCustomerService.remove(new LambdaQueryWrapper<>(SaleMarkedCustomerEntity.class).in(SaleMarkedCustomerEntity::getMarkedPriceId, ids))) {
            return AjaxResult.error("删除销售价目表失败");
        }
        if (!saleMarkedDetailService.remove(new LambdaQueryWrapper<>(SaleMakedDetailEntity.class).in(SaleMakedDetailEntity::getMarkedPriceId, ids))) {
            return AjaxResult.error("删除销售价目表失败");
        }
        boolean removed = this.removeByIds(Arrays.asList(ids));
        if (!removed) {
            return AjaxResult.error("删除销售价目表失败");
        }
        return AjaxResult.success("删除成功");
    }

    /**
     * 删除销售价目信息
     *
     * @param id 销售价目ID
     * @return 结果
     */
    @Override
    public int deleteSaleMarkedPriceById(String id) {
        return saleMarkedPriceMapper.deleteSaleMarkedPriceById(id);
    }

    @Override
    public IPage<SaleMarkedPriceVo> selectSaleMarkedPriceListNew(SaleMarkedDto saleMarkedDto) {

        IPage<SaleMarkedPriceVo> saleMarkedPriceVoIPage = saleMarkedPriceMapper.selectSaleMarkedPriceListNew(saleMarkedDto);
        saleMarkedPriceVoIPage.getRecords().forEach(saleMarkedPriceVo -> {
            //审批人信息
            ActTaskCommentEntity actTaskComment = actTaskCommentService.queryLastComment(saleMarkedPriceVo.getInstanceId());
            Optional.ofNullable(actTaskComment).ifPresent(taskComment -> {
                saleMarkedPriceVo.setApprovedUser(taskComment.getApprovedUser());
                saleMarkedPriceVo.setRemark(taskComment.getRemark());
            });
            ActCurrentTaskVo currentTaskVo = iActModelService.getCurrentTaskByInstanceId(saleMarkedPriceVo.getInstanceId());
            Optional.ofNullable(currentTaskVo).ifPresent(currentTask -> {
                saleMarkedPriceVo.setCurrentTask(currentTask);
            });
        });
        return saleMarkedPriceVoIPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult draft(SaleMarkedPriceEntity saleMarkedPrice) {
        Date nowDate = DateUtils.getNowDate();
        if (StringUtils.isEmpty(saleMarkedPrice.getId())){
            String tenderId = SnowflakeIdWorker.getInstance().nextStringId();
            saleMarkedPrice.setId(tenderId);
            saleMarkedPrice.setCreateTime(DateUtils.getNowDate());
            saleMarkedPrice.setDeptId(CurrentUserUtil.getCompanyId());
            saleMarkedPrice.setState(ConstUtil.STATE_IN_DRAFT);
            List<SaleMakedDetailEntity> saleMakedDetailEntities = saleMarkedPrice.getSaleMakedDetailEntities();
            if (StringUtils.isEmpty(saleMarkedPrice.getPriceCode())) {
                String companyId = CurrentUserUtil.getCompanyId();
                SysDept sysDept = sysDeptService.getById(companyId);
                int count = this.count();
                String countStr = String.format("%06d", count);
                String firstLetter = ChineseToPinyin.getFirstLetter(sysDept.getDeptName());
                String orderNumber = firstLetter.concat("_XSJM_").concat(countStr);
                saleMarkedPrice.setPriceCode(orderNumber);
            }
            if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedCustomerEntities())) {
                saleMarkedPrice.getSaleMarkedCustomerEntities().forEach(s -> {
                    s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                    s.setCreateTime(nowDate);
                    s.setMarkedPriceId(tenderId);
                    s.setType(ConstUtil.SALE_PRICE_TYPE_CUSTOMER);
                    saleMarkedCustomerService.save(s);
                });
            }
            if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedSaleEntities())) {
                saleMarkedPrice.getSaleMarkedSaleEntities().forEach(s -> {
                    s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                    s.setCreateTime(nowDate);
                    s.setMarkedPriceId(tenderId);
                    s.setType(ConstUtil.SALE_PRICE_TYPE_SALE);
                    saleMarkedCustomerService.save(s);
                });
            }
            if (!CollectionUtils.isEmpty(saleMakedDetailEntities)) {
                Map<String, List<SaleMakedDetailEntity>> collect = saleMakedDetailEntities.stream().collect(Collectors.groupingBy(SaleMakedDetailEntity::getCode));
                //循环collect
                collect.forEach((k, list) -> {
                    boolean isValid = list.stream()
                            .allMatch(item -> list.stream()
                                    .noneMatch(other -> other != item &&
                                            item.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().isBefore(other.getLoseDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) &&
                                            item.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().isAfter(other.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate())));
                    if (!isValid) {
                        throw new RuntimeException("价目详情生效日期不能交叉");
                    }
                });
                AtomicInteger i = new AtomicInteger(1);
                saleMakedDetailEntities.forEach(s -> {
                    String nextedId = SnowflakeIdWorker.getInstance().nextStringId();
                    s.setLineState("0");
                    s.setId(nextedId);
                    s.setCreateTime(nowDate);
                    s.setMarkedPriceId(tenderId);
                    i.getAndIncrement();
                    boolean save = saleMarkedDetailService.save(s);
                    if (!save) {
                        throw new RuntimeException("价目表暂存失败");
                    }
                });
            }
        }else {
            if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedCustomerEntities())) {
                boolean remove = saleMarkedCustomerService.remove(new LambdaQueryWrapper<>(SaleMarkedCustomerEntity.class)
                        .eq(SaleMarkedCustomerEntity::getMarkedPriceId, saleMarkedPrice.getId())
                        .eq(SaleMarkedCustomerEntity::getType, ConstUtil.SALE_PRICE_TYPE_CUSTOMER)
                );
                if (!remove) {
                    return AjaxResult.error("删除价目表客户信息失败");
                }
                saleMarkedPrice.getSaleMarkedCustomerEntities().forEach(s -> {
                    s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                    s.setCreateTime(nowDate);
                    s.setMarkedPriceId(saleMarkedPrice.getId());
                    s.setType(ConstUtil.SALE_PRICE_TYPE_CUSTOMER);
                    saleMarkedCustomerService.save(s);
                });
            }
            if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedSaleEntities())) {
                boolean remove = saleMarkedCustomerService.remove(new LambdaQueryWrapper<>(SaleMarkedCustomerEntity.class)
                        .eq(SaleMarkedCustomerEntity::getMarkedPriceId, saleMarkedPrice.getId())
                        .eq(SaleMarkedCustomerEntity::getType, ConstUtil.SALE_PRICE_TYPE_SALE)
                );
                if (!remove) {
                    return AjaxResult.error("删除价目表销售信息失败");
                }

                saleMarkedPrice.getSaleMarkedSaleEntities().forEach(s -> {
                    s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                    s.setCreateTime(nowDate);
                    s.setMarkedPriceId(saleMarkedPrice.getId());
                    s.setType(ConstUtil.SALE_PRICE_TYPE_SALE);
                    saleMarkedCustomerService.save(s);
                });
            }
            List<SaleMakedDetailEntity> saleMakedDetailEntities = saleMarkedPrice.getSaleMakedDetailEntities();
            if (!CollectionUtils.isEmpty(saleMakedDetailEntities)) {
                boolean remove = saleMarkedDetailService.remove(new LambdaQueryWrapper<>(SaleMakedDetailEntity.class).eq(SaleMakedDetailEntity::getMarkedPriceId, saleMarkedPrice.getId()));
                if (!remove) {
                    return AjaxResult.error("删除价目表物料明细失败");
                }
                Map<String, List<SaleMakedDetailEntity>> collect = saleMakedDetailEntities.stream().collect(Collectors.groupingBy(SaleMakedDetailEntity::getCode));
                //循环collect
                collect.forEach((k, list) -> {
                    boolean isValid = list.stream()
                            .allMatch(item -> list.stream()
                                    .noneMatch(other -> other != item &&
                                            item.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().isBefore(other.getLoseDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) &&
                                            item.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().isAfter(other.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate())));
                    if (!isValid) {
                        throw new RuntimeException("价目详情生效日期不能交叉");
                    }
                });
                AtomicInteger i = new AtomicInteger(1);
                saleMakedDetailEntities.forEach(s -> {
                    String nextedId = SnowflakeIdWorker.getInstance().nextStringId();
                    s.setLineState("0");
                    s.setId(nextedId);
                    s.setCreateTime(nowDate);
                    s.setMarkedPriceId(saleMarkedPrice.getId());
                    i.getAndIncrement();
                    boolean save = saleMarkedDetailService.save(s);
                    if (!save) {
                        throw new RuntimeException("价目表暂存失败");
                    }
                });
            }
        }

        boolean save = this.save(saleMarkedPrice);
        if (!save) {
            return AjaxResult.error("价目表暂存失败");
        } else {
            return AjaxResult.success("暂存成功",saleMarkedPrice.getId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult submit(SaleMarkedPriceEntity saleMarkedPrice) {
        if (StringUtils.isNotEmpty(saleMarkedPrice.getId())) {
            return AjaxResult.error("请先暂存单据");
        }
        Date nowDate = DateUtils.getNowDate();
        StartProcessDto startDto = new StartProcessDto().setBusinessId(saleMarkedPrice.getId()).setBusinessType(saleMarkedPrice.getBusinessType()).setModelId(saleMarkedPrice.getModelId())
                .setBusinessName("销售价目表_".concat(saleMarkedPrice.getId()));
        String instanceId = actModelService.startProcess(startDto);
        saleMarkedPrice.setInstanceId(instanceId);
        saleMarkedPrice.setUpdateTime(nowDate);
        if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedCustomerEntities())) {
            //先删除再添加
            saleMarkedCustomerService.remove(new LambdaQueryWrapper<>(SaleMarkedCustomerEntity.class).eq(SaleMarkedCustomerEntity::getMarkedPriceId, saleMarkedPrice.getId()));
            saleMarkedPrice.getSaleMarkedCustomerEntities().forEach(s -> {
                s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(saleMarkedPrice.getId());
                s.setType(ConstUtil.SALE_PRICE_TYPE_CUSTOMER);
            });
            boolean saved = saleMarkedCustomerService.saveBatch(saleMarkedPrice.getSaleMarkedCustomerEntities());
            if (!saved) {
                return AjaxResult.error("价目表提交失败");
            }
        }
        if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMarkedSaleEntities())) {
            //先删除再添加
            saleMarkedCustomerService.remove(new LambdaQueryWrapper<>(SaleMarkedCustomerEntity.class).eq(SaleMarkedCustomerEntity::getMarkedPriceId, saleMarkedPrice.getId()));
            saleMarkedPrice.getSaleMarkedSaleEntities().forEach(s -> {
                s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(saleMarkedPrice.getId());
                s.setType(ConstUtil.SALE_PRICE_TYPE_SALE);
            });
            boolean saved = saleMarkedCustomerService.saveBatch(saleMarkedPrice.getSaleMarkedSaleEntities());
            if (!saved) {
                return AjaxResult.error("价目表提交失败");
            }
        }
        if (!CollectionUtils.isEmpty(saleMarkedPrice.getSaleMakedDetailEntities())) {
            //先删除再添加
            saleMarkedDetailService.remove(new LambdaQueryWrapper<>(SaleMakedDetailEntity.class).eq(SaleMakedDetailEntity::getMarkedPriceId, saleMarkedPrice.getId()));
            saleMarkedPrice.getSaleMakedDetailEntities().forEach(s -> {
                s.setId(SnowflakeIdWorker.getInstance().nextStringId());
                s.setCreateTime(nowDate);
                s.setMarkedPriceId(saleMarkedPrice.getId());
            });
            boolean saved = saleMarkedDetailService.saveBatch(saleMarkedPrice.getSaleMakedDetailEntities());
            if (!saved) {
                return AjaxResult.error("价目表提交失败");
            }
        }
        saleMarkedPrice.setState(ConstUtil.STATE_IN_PROCESS);
        boolean update = this.updateById(saleMarkedPrice);
        if (!update) {
            return AjaxResult.error("价目表提交失败");
        }
        return AjaxResult.success("提交成功");
    }
}
